home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15286 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: li.net!jeremy
  2. From: jeremy@newshost.li.net (Jeremy Markman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Is this a memory leak?
  5. Date: 4 Apr 1996 17:32:58 GMT
  6. Organization: LI Net (Long Island Network)
  7. Message-ID: <4k114a$m5d@linet06.li.net>
  8. References: <4jv214$gv7@insosf1.netins.net>
  9. NNTP-Posting-Host: linet04.li.net
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Harold Howe (hhowe@trgnet.com) wrote:
  13. : Could someone please tell me if this code leaks memory
  14.  
  15.  
  16. :   public:
  17. :     TopClass::TopClass() { bury = new BuriedClass();}
  18. :     shutDown             { bury = 0;}
  19. :     ~TopClass            { delete bury}
  20.  
  21. : Is the memory that was alloced for the buried class lost? If not please 
  22. : describe how it was freed.  Does zeroing the pointer free the memory? The 
  23. : delete is a waste of time on a zeroed pointer, isn't it.
  24.  
  25. Yes, your are correct.  By assigning bury to 0, you are effectively 
  26. reassigning the memory pointer to NULL.  Therefore, deleting bury has no 
  27. effect.  The shutdown function is not necessary, in fact, it is bad 
  28. practice to do that.  Just the delete bury is necessary, and if you do 
  29. want to assign 0 to a pointer, use NULL.
  30.  
  31. More explanation:
  32. new allocattes memory and delete deallocates it.  If you change the 
  33. pointer to that it does not point to the allocated memory, it can't 
  34. deallocate it, now can it?
  35.  
  36. Hope this helps...
  37.